package com.yunchang.springboot.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: lindw
 * @Date: 2020/03/08 14:09
 * @Version 1.0
 */
@Configuration
@EnableAsync
public class AsyncTaskConfig implements AsyncConfigurer {
    /** ThredPoolTaskExcutor的处理流程
     *  当池子大小小于corePoolSize，就新建线程，并处理请求
     *  当池子大小等于corePoolSize，把请求放入workQueue中，池子里的空闲线程就去workQueue中取任务并处理
     *  当workQueue放不下任务时，就新建线程入池，并处理请求，如果池子大小撑到了maximumPoolSize，就用RejectedExecutionHandler来做拒绝处理
     *  当池子的线程数大于corePoolSize时，多余的线程会等待keepAliveTime长时间，如果无请求可处理就自行销毁
     */
    @Override
    public Executor getAsyncExecutor() {
        // 新建一个任务执行器
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        // 设置核心的线程数量
        taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
        // 设置最大的线程数量
        taskExecutor.setMaxPoolSize(Runtime.getRuntime().availableProcessors());
        // 等待队列
        taskExecutor.setQueueCapacity(2000);
        //线程前缀名称
        taskExecutor.setThreadNamePrefix("tsak-asyn");
        //配置拒绝策略
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //配置多线程关闭子线程
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        // 初始化
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}
